const CONFIG_THEME = {
    storageKey: 'app-theme',
    defaultTheme: 'cosmic',
    themeCssId: 'theme-css',
    themeCssPath: '/css/theme/theme-'
};
let currentTheme = null;

function restoreSavedTheme() {
    // 尝试从本地存储获取主题，若无则使用默认值
    currentTheme = localStorage.getItem(CONFIG_THEME.storageKey) || CONFIG_THEME.defaultTheme;
    console.log('恢复主题:', currentTheme);
}

function loadThemeCss(theme) {
    const href = `${CONFIG_THEME.themeCssPath}${theme}.css`;
    let $link = $(`#${CONFIG_THEME.themeCssId}`);

    if ($link.length === 0) {
        $link = $('<link>')
            .attr({
                id: CONFIG_THEME.themeCssId,
                rel: 'stylesheet',
                type: 'text/css',
                href: href
            })
            .appendTo('head');
        console.log('创建新主题链接:', href);
    } else {
        $link.attr('href', href);
        console.log('更新主题链接:', href);
    }
}

function switchTheme(theme) {
    if (!theme || currentTheme === theme) return;

    // 移除旧主题 class
    $('body').removeClass(`theme-${currentTheme}`);
    // 添加新主题 class
    $('body').addClass(`theme-${theme}`);
    // 加载 CSS 文件
    loadThemeCss(theme);
    // 更新当前主题
    currentTheme = theme;
    localStorage.setItem(CONFIG_THEME.storageKey, theme);
    // 更新按钮高亮
    $('#themeSelector button').removeClass('active');
    $(`#themeSelector button[data-theme="${theme}"]`).addClass('active');

    console.log('已切换到主题:', theme);
}

function bindEvents_switchTheme() {
    $(document).on('click', '#themeSelector button[data-theme]', function () {
        const theme = $(this).data('theme');
        switchTheme(theme);
    });
}

function initTheme() {
    console.log('开始初始化主题...');
    restoreSavedTheme();
    // 确保body添加当前主题类
    $('body').addClass(`theme-${currentTheme}`);
    loadThemeCss(currentTheme);
    bindEvents_switchTheme();
    // 设置当前按钮高亮
    $(`#themeSelector button[data-theme="${currentTheme}"]`).addClass('active');
    console.log('主题初始化完成，当前主题:', currentTheme);
}

function displayThemeSelector() {
    const themeSelector = document.getElementById('themeSelector');
    if (themeSelector) {
        themeSelector.hidden = !themeSelector.hidden;
    }
}

// 生成星星背景
function createStars() {
    const starsContainer = $('#stars')[0]; // 获取原生DOM元素
    // 或者使用原生方法获取：const starsContainer = document.getElementById('stars');
    if (!starsContainer) return;
    starsContainer.innerHTML = ''; // 清空现有内容
    for (let i = 0; i < 100; i++) {
        // 创建星星元素（使用原生方法）
        const star = document.createElement('div');
        star.className = 'star';

        const size = Math.random() * 2 + 'px';
        star.style.width = size;
        star.style.height = size;
        star.style.left = Math.random() * 100 + 'vw';
        star.style.top = Math.random() * 100 + 'vh';
        star.style.animationDelay = Math.random() * 5 + 's';

        starsContainer.appendChild(star); // 现在可以正常使用appendChild了
    }
};

// 确保DOM完全加载后初始化主题
$(document).ready(function() {
    console.log('DOM就绪，准备初始化主题');
    initTheme();
    createStars();
});
